package com.jbp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.exception.RRException;
import com.jbp.model.Identify;
import com.jbp.model.ReciverAddress;
import com.jbp.service.IdentifyService;
import com.jbp.service.ReciverAddressService;
import com.jbp.service.TencentIdentifyService;
import com.jbp.utils.IdNumberUtil;
import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;

/**
 * 实名认证(Identify)表控制层
 * @author cc
 * @since 2020-06-20 21:43:48
 */
@Api(tags ="APP接口-用户-实名认证" ,hidden = true)
@ApiIgnore
@RestController
@RequestMapping("rest/identify")
public class IdentifyController {
    @Autowired
    private IdentifyService identifyService;
    @Autowired
    private ReciverAddressService reciverAddressService;
    @Autowired
    private TencentIdentifyService tencentIdentifyService;
    /** 保存 */
    @ApiOperation(value="实名认证")
    @PostMapping("/save")
    public R save(@RequestBody Identify identify,@RequestAttribute("userId") Integer userId){
        if (identify.getIdNumber() == null || identify.getRealName() == null || identify.getRealName().length()<1){
            return R.error("参数错误");
        }
        identifyService.saveIdNumber(userId, identify);
        return R.ok();
    }

    @PostMapping("/createOrder")
    public R createOrder(@RequestBody Identify identify,@RequestAttribute("userId") Integer userId){
        if (identify.getIdNumber() == null || identify.getRealName() == null || identify.getRealName().length()<1){
            return R.error("参数错误");
        }

        Map<String, Object> map = identifyService.creatOrder(identify, userId);

        return R.ok(map);
    }

    @PostMapping("/success")
    public R success(@RequestAttribute("userId") Integer userId){
        identifyService.succes(userId);
        return R.ok();
    }

    /** 保存 */
    @ApiOperation(value="支付宝绑定")
    @PostMapping("/saveReciverAddress")
    public R saveReciverAddress(@RequestBody ReciverAddress address, @RequestAttribute("userId") Integer userId){
        if (address.getAddress() == null){
            return R.error("参数错误");
        }
        reciverAddressService.saveReciverAddress(userId, address);
        return R.ok();
    }

    /** 保存 */
    @ApiOperation(value="实名认证信息", notes = "realName 姓名， idNumber 身份证号， aliAddress 支付宝")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @PostMapping("/realNameInfo")
    public R realNameInfo(@RequestAttribute("userId") Integer userId){
        String realName = null;
        String idNumber = null;
        String aliAddress = null;
        int status = 0;

        //获取身份证信息
        QueryWrapper<Identify> identifyQueryWrapper = new QueryWrapper<>();
        identifyQueryWrapper.eq("user_id", userId);
        Identify identify = identifyService.getOne(identifyQueryWrapper);
        if (identify!=null) {
            realName = identify.getRealName();
            idNumber = identify.getIdNumber();
            status = identify.getStatus();
        }

        ///获取收款地址信息
        QueryWrapper<ReciverAddress> reciverAddressQueryWrapper = new QueryWrapper<>();
        reciverAddressQueryWrapper.eq("user_id", userId);
        ReciverAddress reciverAddress = reciverAddressService.getOne(reciverAddressQueryWrapper);
        if (reciverAddress!=null) {
            aliAddress = reciverAddress.getAddress();
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("realName", realName);
        map.put("idNumber", idNumber);
        map.put("status", status);
        map.put("aliAddress", aliAddress);
        return R.ok(map);
    }
}